#include <iostream>
#include <queue>
using namespace std;



struct edge{
	int stop,len,next;
};



int main(){
	int n,m,s,inf=2147483647;
	cin>>n>>m>>s;
	s = s-1;
	bool tf[n];
	edge edges[m];
	int edge_header[n],fast[n];
	queue<int> q;
	for(int i=0;i<n;i++){
		tf[i]=true;
		edge_header[i]=-1;
		fast[i] = inf;
	}
	for(int i=0;i<m;i++){
		int start,stop,len;
		cin>>start>>stop>>len;
//		edges[i]={stop-1,len,edge_header[start-1]};
		edges[i].len=len;
		edges[i].next=edge_header[start-1];
		edges[i].stop=stop-1;
		edge_header[start-1] = i;
	}
	
	tf[s] = false;
	q.push(s);
	fast[s] = 0;
	while(!q.empty()){
		tf[q.front()]=true;
		int l=edge_header[q.front()],len=fast[q.front()];
		q.pop();
		while(l!=-1){
			int stop,so_len;
			stop = edges[l].stop;
			so_len = edges[l].len; 
			if(fast[stop]>len+so_len){
				fast[stop] = len+so_len;
				if(tf[stop]){
					q.push(stop);
					tf[stop] = false;
				}
			}
			l = edges[l].next;
		}
	}
	for(int i=0;i<n;i++){
		cout<<fast[i]<<' ';
	}
}
